<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="设备id" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="绑定时间" prop="bindTime">
        <el-date-picker clearable
          v-model="queryParams.bindTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择绑定时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:customerDeviceBind:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:customerDeviceBind:remove']"
        >解绑</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:customerDeviceBind:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerDeviceBindList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备编号" align="center" prop="deviceNo"/>
      <el-table-column label="设备状态" align="center" prop="situation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_situation" :value="scope.row.situation"/>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="name"/>
      <el-table-column label="设备类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="设备描述" align="center" prop="remark"/>
      <el-table-column label="绑定用户名称" align="center" prop="customerName"/>
      <el-table-column label="绑定时间" align="center" prop="bindTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bindTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备归属" align="center" prop="ownershipFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_ownership_flag" :value="scope.row.ownershipFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:customerDeviceBind:remove']"
          >解绑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :visible.sync="popupVisible" title="设备列表" width="90%" append-to-body @close='closeBind'>
      <DeviceSelect :customer-id="form.customerId" :popupVisible="popupVisible"></DeviceSelect>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCustomerDeviceBind,
  delCustomerDeviceBind,
  getCustomerDeviceBind,
  listCustomerDeviceBind,
  updateCustomerDeviceBind
} from '@/api/customer/customerDeviceBind'

export default {
  name: "CustomerDeviceBind",
  dicts: ['delete_flag', 'device_type','device_situation','device_ownership_flag'],
  data() {
    return {
      popupVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户设备绑定表格数据
      customerDeviceBindList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        deviceId: null,
        bindTime: null,
        deviceType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const customerId=this.$route.params && this.$route.params.customerId
    this.form.customerId=customerId
    this.queryParams.customerId=customerId
    this.getList();
  },
  methods: {
    closeBind(){
      this.popupVisible=false
      this.getList()
    },
    /** 查询用户设备绑定列表 */
    getList() {
      this.loading = true;
      listCustomerDeviceBind(this.queryParams).then(response => {
        this.customerDeviceBindList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        deviceId: null,
        bindTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deviceType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.popupVisible = true;
    },
    /** 新增按钮操作 */
    handleAdd2() {
      this.reset();
      this.open = true;
      this.title = "添加用户设备绑定";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomerDeviceBind(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户设备绑定";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认解绑用户设备绑定编号为"' + ids + '"的数据项？').then(function() {
        return delCustomerDeviceBind(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("解绑成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/customerDeviceBind/export', {
        ...this.queryParams
      }, `customerDeviceBind_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
